﻿using System;
using System.Net;
using System.Xml.Serialization;
using FlashCards.Dto;
using System.IO;
using FlashCards.SL.FlashCardRepeater.DataServices;

namespace FlashCards.SL.FlashCardRepeater.ViewModels
{
    public class LoadingViewModel : BaseViewModel
    {
        private App _application;

        private double _loadingProgress;
        public double LoadingProgress
        {
            get { return _loadingProgress; }
            set { _loadingProgress = value; OnPropertyChanged("LoadingProgress"); }
        }

        bool _isUserDataLoaded;
        bool _isFlashcardDataLoaded;
        bool _isTypesDataLoaded;
        bool _isRepeatDataLoaded;

        #region EVENTS

        public event EventHandler<DataLoadedEventArgs> UserRepeatsLoadCompleted;

        #endregion

        public LoadingViewModel(string endpointName)
        {
            _application = App.Current as App;
            var proxy = new DataServices.SilverlightServiceClient(endpointName);
            StartLoadData(proxy);
        }


        void StartLoadData(SilverlightServiceClient proxy)
        {
            StartLoadUserData(proxy);
            StartLoadFlashcardData(proxy);
            StartLoadFlashcardPartTypes(proxy);
            StartLoadUserRepeatItems(proxy);
        }

        void StartLoadUserData(SilverlightServiceClient proxy)
        {
            proxy.GetUserDataCompleted += HandleLoadUserDataCompleted;
            proxy.GetUserDataAsync(App.UserId);
        }

        void HandleLoadUserDataCompleted(object sender, DataServices.GetUserDataCompletedEventArgs e)
        {
            LoadingProgress += 25;
            
            _application.LoadUserData(e.Result);
            _isUserDataLoaded = true;
            //StartLoadFlashcardData();
            CheckForLoadingEnd();
        }

        void StartLoadFlashcardData(SilverlightServiceClient proxy)
        {
            proxy.GetFlashcardSetCompleted += HandleLoadFlashcardDataCompleted;
            proxy.GetFlashcardSetAsync(App.FlashcardSetId);
        }

        void HandleLoadFlashcardDataCompleted(object sender, DataServices.GetFlashcardSetCompletedEventArgs e)
        {
            LoadingProgress += 25;

            _application.LoadFlashcardData(e.Result);
            _isFlashcardDataLoaded = true;
            //StartLoadFlashcardPartTypes();
            CheckForLoadingEnd();
        }

        void StartLoadFlashcardPartTypes(SilverlightServiceClient proxy)
        {
            proxy.GetFlashcardPartTypesCompleted += HandleLoadFlashcardPartTypesCompleted;
            proxy.GetFlashcardPartTypesAsync();
        }

        void HandleLoadFlashcardPartTypesCompleted(object sender, DataServices.GetFlashcardPartTypesCompletedEventArgs e)
        {
            LoadingProgress += 25;

            _application.LoadFlashcardPartTypes(e.Result);
            _isTypesDataLoaded = true;
            //StartLoadUserRepeatItems();
            CheckForLoadingEnd();
        }

        void StartLoadUserRepeatItems(SilverlightServiceClient proxy)
        {
            proxy.GetUserLastRepeatCompleted += HandleGetUserRepeatCompleted;
            proxy.GetUserLastRepeatAsync(App.UserId, App.FlashcardSetId);
        }

        void HandleGetUserRepeatCompleted(object sender, DataServices.GetUserLastRepeatCompletedEventArgs e)
        {
            LoadingProgress += 25;
            if (UserRepeatsLoadCompleted != null)
            {
                UserRepeatsLoadCompleted.Invoke(this, new DataLoadedEventArgs(e.Result));
            }
            _isRepeatDataLoaded = true;
            CheckForLoadingEnd();
        }

        void CheckForLoadingEnd()
        {
            if(IsAllLoaded())
                _application.FinishDataLoading(null);
        }

        bool IsAllLoaded()
        {
            return (_isFlashcardDataLoaded && _isRepeatDataLoaded && _isTypesDataLoaded && _isUserDataLoaded);
        }

    }

    public class DataLoadedEventArgs : EventArgs
    {
        public string SerializedString { get; set; }

        public DataLoadedEventArgs(string data)
        {
            SerializedString = data;
        }
    }
}
